package com.pm.web.controller.buglist;


import com.pm.buglist.domain.CompatibleStation;
import com.pm.buglist.service.CompatibleStationService;
import com.pm.common.annotation.Log;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.core.page.TableDataInfo;
import com.pm.common.core.redis.RedisCache;
import com.pm.common.enums.BusinessType;
import com.pm.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 兼容站台管理表 前端控制器
 * </p>
 *
 * @author wwm
 * @since 2023-12-14
 */
@RestController
@RequestMapping("/business/compatible-station")
public class CompatibleStationController extends BaseController {

    @Autowired
    private RedisCache redisCache;

    @Resource
    private CompatibleStationService compatibleStationService;

    /**
     * 查询兼容站台管理列表
     */
//    @PreAuthorize("@ss.hasPermi('business:station:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompatibleStation compatibleStation)
    {
        startPage();
        List<CompatibleStation> list = compatibleStationService.selectCompatibleStationList(compatibleStation);
        return getDataTable(list);
    }

    @GetMapping("/listTwo")
    public AjaxResult listTwo(){
        String key ="CPS_CODE";
        List<Object> cacheList = redisCache.getCacheList(key);
        if(CollectionUtils.isEmpty(cacheList)){
            ReentrantLock locks = new ReentrantLock();
            try {
                //加锁
                locks.lock();
                cacheList = redisCache.getCacheList(key);
                if(CollectionUtils.isEmpty(cacheList)){
                List<CompatibleStation> compatibleStations = compatibleStationService.selectCompatibleStationList(null);
                if(!CollectionUtils.isEmpty(compatibleStations)){
                    redisCache.setCacheList(key, compatibleStations);
                }
                return AjaxResult.success(compatibleStations);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.error("查找数据站台数据异常！！！");
            }finally {
                //释放锁
                locks.unlock();

            }
        }

        return AjaxResult.success(cacheList);
    }

    /**
     * 导出兼容站台管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:station:export')")
    @Log(title = "兼容站台管理:导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CompatibleStation compatibleStation)
    {
        List<CompatibleStation> list = compatibleStationService.selectCompatibleStationList(compatibleStation);
        ExcelUtil<CompatibleStation> util = new ExcelUtil<CompatibleStation>(CompatibleStation.class);
        util.exportExcel(response, list, "兼容站台管理数据");
    }

    /**
     * 获取兼容站台管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('business:station:query')")
    @GetMapping(value = "/query/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(compatibleStationService.selectCompatibleStationById(id));
    }

    /**
     * 新增兼容站台管理
     */
    @PreAuthorize("@ss.hasPermi('business:station:add')")
    @Log(title = "兼容站台管理:新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CompatibleStation compatibleStation)
    {
        return toAjax(compatibleStationService.insertCompatibleStation(compatibleStation));
    }

    /**
     * 修改兼容站台管理
     */
    @PreAuthorize("@ss.hasPermi('business:station:update')")
    @Log(title = "兼容站台管理:修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody CompatibleStation compatibleStation)
    {
        return toAjax(compatibleStationService.updateCompatibleStation(compatibleStation));
    }

    /**
     * 删除兼容站台管理
     */
    @PreAuthorize("@ss.hasPermi('business:station:remove')")
    @Log(title = "兼容站台管理:删除", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(compatibleStationService.deleteCompatibleStationByIds(ids));
    }

}

